<?php
namespace App\Controller\Admin;

use App\Query\ProductQuery;
use App\Repository\ProductRepository;
use App\Request\ProductRequest;

class ProductController extends Controller
{
    // 列表
    public function index()
    {
        return ProductQuery::model()->get();
    }
    
    // 新增
    public function create(ProductRequest $request)
    {
        if(ProductRepository::make()->save($request->validated()))
        {
            $this->message('create');
        }
        
        $this->error('create');
    }
    
    // 更新
    public function update($id, ProductRequest $request)
    {
        $model = ProductRepository::find($id);

        if($model && $model->save($request->validated()))
        {
            $this->message('update');
        }
        
        $this->error('update');
    }
    
    // 删除
    public function delete($id)
    {
        $model = ProductRepository::find($id);

        if($model && $model->delete())
        {
            $this->message('delete');
        }
        
        $this->error('delete');
    }
}